package com.tomecode.tools.maven.settings.gui.compents;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;

import com.tomecode.tools.maven.settings.model.UpdatePolicy;
import com.tomecode.util.gui.PanelFactory;

/**
 * 
 * Componet for {@link UpdatePolicy}
 * 
 * @author Frastia Tomas
 * 
 */
public final class UpdatePolicyPanel extends JPanel {

	private static final long serialVersionUID = -7383066453552329183L;

	private final JComboBox comboBox;

	private final JPanel pSpinner;

	private final JSpinner spinnerDay;
	private final JSpinner spinnerHour;
	private final JSpinner spinnerMin;

	public UpdatePolicyPanel() {
		setLayout(new BorderLayout());
		// setPreferredSize(new Dimension(100, 20));
		pSpinner = new JPanel(new BorderLayout());
		pSpinner.setBorder(new EmptyBorder(-4, 0, 0, 0));
		// pSpinnersetPreferredSize(new Dimension(100, 20));
		spinnerDay = new JSpinner(new SpinnerNumberModel(0, 0, 3600, 1));
		spinnerHour = new JSpinner(new SpinnerNumberModel(0, 0, 24, 1));
		spinnerMin = new JSpinner(new SpinnerNumberModel(0, 0, 60, 1));

		JPanel pSp = PanelFactory.createButtonsPanel(0, 0, 0, 0, PanelFactory.wrapWithLabelNorm("D", spinnerDay, 0), PanelFactory.wrapWithLabelNorm("HH", spinnerHour, 0), PanelFactory.wrapWithLabelNorm("MM", spinnerMin, 0));
		pSpinner.add(PanelFactory.wrapByBorderLayout(pSp, BorderLayout.NORTH), BorderLayout.CENTER);

		comboBox = new JComboBox(UpdatePolicy.values());
		comboBox.addItemListener(new ItemListener() {
			public final void itemStateChanged(ItemEvent e) {
				if (e.getItem() == UpdatePolicy.INTERVAL) {
					showInterval();
				} else {
					showNormal();
				}
			}
		});

		add(comboBox, BorderLayout.CENTER);
	}

	public final void selectPolicy(Object updatePolicy) {
		comboBox.setSelectedItem(updatePolicy);
	}

	private final void showNormal() {
		for (int i = 0; i <= getComponentCount() - 1; i++) {
			if (getComponent(i) instanceof JPanel) {
				remove(i);
				break;
			}
		}
		updateUI();
	}

	/**
	 * show interval panel
	 */
	private final void showInterval() {
		for (int i = 0; i <= getComponentCount() - 1; i++) {
			if (getComponent(i) instanceof JPanel) {
				return;
			}
		}
		add(pSpinner, BorderLayout.EAST);
		updateUI();
	}

	public final String getSelectedItem() {
		if (comboBox.getSelectedItem() == UpdatePolicy.INTERVAL) {

			int day = ((Integer) spinnerDay.getValue()) * (24 * 60);
			int hour = ((Integer) spinnerHour.getValue()) * 60;
			int min = ((Integer) spinnerMin.getValue());

			return UpdatePolicy.getInterval((day + hour + min));
		}
		return comboBox.getSelectedItem().toString();
	}

	public final void selectInteval(int updatePolicyInterval) {
		if (updatePolicyInterval == 0) {
			spinnerDay.setValue(0);
			spinnerHour.setValue(0);
			spinnerMin.setValue(0);
		} else if (updatePolicyInterval <= 60) {
			spinnerDay.setValue(0);
			spinnerHour.setValue(0);
			spinnerMin.setValue(updatePolicyInterval);
		} else if (updatePolicyInterval > 60 && updatePolicyInterval <= 1440) {
			spinnerDay.setValue(0);
			int hours = updatePolicyInterval / 60;
			spinnerHour.setValue(hours);
			spinnerMin.setValue(updatePolicyInterval - (60 * hours));
		} else {
			int days = updatePolicyInterval / (24 * 60);
			spinnerDay.setValue(days);
			updatePolicyInterval = updatePolicyInterval - ((24 * 60) * days);
			int hours = updatePolicyInterval / 60;
			spinnerHour.setValue(hours);
			spinnerMin.setValue(updatePolicyInterval - (60 * hours));
		}
	}
}
